<?php
namespace app\jobs;

use app\config\Constants;
use app\models\GoodsOrder;
use app\models\Merchant;
use app\utils\WebSocketUtil;

/**
 * WebSocket任务
 * Class AutoAcceptJob
 * @package app\jobs
 */
class WebSocketJob {

    private static $handle_type = [Constants::ORDER_WAIT];

    public function execute($queue,$job){
        if($job['msg_type'] != Constants::MSG_TYPE_ORDER){
            return true;
        }
        if(!in_array($job['event_type'],self::$handle_type)){
            return true;
        }

        $order = GoodsOrder::findOne(['order_no'=>$job['data']['order_no']]);
        if(!$order){
            return false;
        }
        $merchant = Merchant::findOne(['merchant_id'=>$order->merchant_id]);
        if(!$merchant || $merchant->auto_accept == Constants::STATUS_NORMAL){
            return true;
        }
        
        return WebSocketUtil::send(Constants::MSG_TYPE_ORDER,Constants::ORDER_WAIT,$order->merchant_id,$order);
    }
}